package lc.oa.ui.fragment;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import java.util.Calendar;

import lc.oa.App;
import lc.oa.R;
import lc.oa.data.model.LoggedInUser;
import lc.oa.echats.EchartOptionUtil;
import lc.oa.echats.EchartView;
import lc.oa.ui.base.BaseFragment;

/**
 * @Description: 业绩(新)处理逻辑
 * @Author: lucifer
 * @CreateDate: 2020/8/7 16:13
 * @UpdateUser:
 * @UpdateDate:
 * @UpdateRemark:
 * @Version: 1.0
 */
public class TestFragment extends BaseFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private EchartView lineChart;
    private TextView month_check;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
        // bindUserInfo(view);

        lineChart = view.findViewById(R.id.lineChart);
        bindEvent(view.getContext());
        month_check = view.findViewById(R.id.month_check);
        month_check.setOnClickListener(this);
        return view;
    }

    private ProgressDialog mProgressDialog;

    public ProgressDialog getmProgressDialog() {
        return mProgressDialog;
    }

    private void bindEvent(Context context) {
        mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setTitle("loading");
        lineChart.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //最好在h5页面加载完毕后再加载数据，防止html的标签还未加载完成，不能正常显示
                refreshLineChart();
                mProgressDialog.hide();
            }

            @SuppressLint("LongLogTag")
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.i("------onPageStarted-----------------", "------onPageStarted-----------------");
                mProgressDialog.show();
            }
        });
//        lineChart.setWebViewClient(new WebViewClient(){
//            @SuppressLint("LongLogTag")
//            @Override
//            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                super.onPageStarted(view, url, favicon);
//                Log.i("------onPageStarted-----------------","------onPageStarted-----------------");
//                mProgressDialog.show();
//            }
//
//            @SuppressLint("LongLogTag")
//            @Override
//            public void onPageFinished(WebView view, String url) {
//                super.onPageFinished(view, url);
//                Log.i("------onPageFinished-----------------","------onPageFinished-----------------");
//                mProgressDialog.hide();
//            }
//        });
    }

    private void refreshLineChart() {
        lineChart.refreshEchartsWithOption(EchartOptionUtil.getPieChartOptions());
    }

    private void bindUserInfo(View view) {
        LoggedInUser users = App.getLoggedInUser();
//       if (null!=users){
//           TextView tvUserName=view.findViewById(R.id.tv_username);
//           TextView tvEmployee=view.findViewById(R.id.tv_EmployeeNo);
//           TextView tvMobile=view.findViewById(R.id.tv_Mobile);
//           TextView tvPositionName=view.findViewById(R.id.tvPositionName);
//           TextView tvEmail=view.findViewById(R.id.tvEmail);
//           TextView tvDepName=view.findViewById(R.id.tv_DepName);
//
//           //TextView tvUserName=view.findViewById(R.id.tv_username);
//           tvUserName.setText(users.getUserName());
//           tvEmployee.setText(users.getEmployeeNo());
//           tvMobile.setText(users.getMobile());
//           tvPositionName.setText(users.getPositionName());
//           tvEmail.setText(users.getEmail());
//           tvDepName.setText(users.getDepName());
//       }
    }

    @Override
    protected void handlerMessage1(Message msg) {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initControl() {

    }

    @Override
    public void onModeChanged(int action, Object... values) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mProgressDialog.dismiss();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.month_check:
                Log.e("触发", "点击");
                changeTime();
                break;
        }
    }

    //时间切换
    private void changeTime() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);       //获取年月日时分秒
        Log.i("wxy", "year" + year);
        int month = cal.get(Calendar.MONTH);   //获取到的月份是从0开始计数
        int day = cal.get(Calendar.DAY_OF_MONTH);

        DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker arg0, int year, int month, int day) {
                int mon = ++month;
                Toast.makeText(getContext(), year + "-" + mon + "-" + day, Toast.LENGTH_SHORT).show();     //将选择的日期显示到TextView中,因为之前获取month直接使用，所以不需要+1，这个地方需要显示，所以+1

                month_check.setText(year + "-" + (mon < 10 ? "0" + mon : mon));
            }
        };
        DatePickerDialog dialog = new DatePickerDialog(getContext(), DatePickerDialog.THEME_HOLO_LIGHT, listener, year, month, day);//主题在这里！后边三个参数为显示dialog时默认的日期，月份从0开始，0-11对应1-12个月

        dialog.show();
    }
}
